<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="应聘者">
              <j-search-select-tag
                placeholder="请选择应聘者"
                v-model="queryParam.candidateId"
                dict="HrCandidateInfo,name,id,"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="职位">
              <j-dict-select-tag
                placeholder="请选择职位"
                v-model="queryParam.position"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =POSITION"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="状态">
              <j-dict-select-tag
                placeholder="请选择状态"
                v-model="queryParam.statusId"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = APPLY_STATUS"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button> -->
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('入职通知')">导出</a-button> -->
      <!-- <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="download"   @click="handleExportXlsTemplate('OFFER','images/model/offer.xls')" >下载模板</a-button> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <!-- <a-menu-item key="2" @click="handleExportXls"><a-icon type="download" />导出</a-menu-item> -->
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
        :scroll="{ x: 2000 }"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleView(record)">查看</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-if="record.statusId == '0'">
                <a-popconfirm title="确定提交审批吗?" @confirm="() => handleSubmit(record.id)">
                  <a>提交审批</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if="record.statusId == '1'">
                <a @click="handleSteps(record.id)">查看进度</a>
              </a-menu-item>
              <a-menu-item v-if="record.statusId == '2' || record.statusId == '3'">
                <a @click="handleSteps(record.id)">查看记录</a>
              </a-menu-item>
              <a-menu-item v-if="record.statusId == '2' && record.isConfirmOffer == 'N'">
                <a @click="handleSend(record)">发送邮件</a>
              </a-menu-item>
              <a-menu-item v-if="record.statusId == '2' && record.isConfirmOffer =='N'">
                <a @click="handleConfirmOffer(record)">是否接受OFFER</a>
              </a-menu-item>
              <a-menu-item v-if="record.statusId == '0'">
                <a @click="handleEdit(record)">编辑</a>
              </a-menu-item>
              <a-menu-item v-if="record.statusId == '0'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <hrOffer-modal ref="modalForm" @ok="modalFormOk"></hrOffer-modal>

    <appro-modal :data2="data2" :apprVisible="apprVisible" @apprClose="apprVisible = false"></appro-modal>

    <a-modal title="发送邮件" :visible="emailVisible" @ok="sendEmail" @cancel="emailVisible = false" okText="发送">
      <p v-html="content"></p>
    </a-modal>

    <a-modal
      title="是否接受OFFER"
      :visible="confirmOfferVisible"
      @ok="confirmOffer"
      @cancel="confirmOfferVisible = false"
      okText="提交"
    >
      <a-row>
        <a-col :span="24">
          <a-form-item label="应聘者" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="true" v-model="name"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否已接收" :labelCol="labelCol" :wrapperCol="wrapperCol">
             <a-select v-model="isConfirmOffer" placeholder="请选择" style="width: 100%">
            <a-select-option value="Y">是</a-select-option>
            <a-select-option value="N">否</a-select-option>
          </a-select>
          </a-form-item>
        </a-col>
      </a-row>
    </a-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HrOfferModal from './modules/HrOfferModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import ApproModal from '@/components/jeecg/modal/approModal'
import { getAction } from '@/api/manage'
import { connect } from 'echarts/core'

export default {
  name: 'HrOfferList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    JSearchSelectTag,
    HrOfferModal,
    ApproModal,
  },
  data() {
    return {
      data2: [],
      apprVisible: false,
      emailVisible: false,
      confirmOfferVisible: false,
      content: '',
      description: '入职通知管理页面',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 },
      },
      dataParam: {
        createBu: this.$store.getters.buData == 'allhrdata' ? '' : this.$store.getters.departmentId,
      },
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          fixed: 'left',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '应聘者',
          align: 'center',
          width: 100,
          dataIndex: 'candidateId_dictText',
          fixed: 'left',
          sorter: (a,b) => a.candidateId_dictText -b.candidateId_dictText,
        },
        {
          title: '公司',
          align: 'center',
          dataIndex: 'company_dictText',
          sorter: (a,b) => a.company_dictText -b.company_dictText,
        },
        {
          title: '部门',
          align: 'center',
          width: 120,
          dataIndex: 'departmentId',
          customRender: (text, record) => (text ? record['departmentId_dictText'] : ''),
          sorter: (a,b) => a.departmentId -b.departmentId,
        },
        {
          title: '职位',
          align: 'center',
          width: 120,
          dataIndex: 'position',
          sorter: (a,b) => a.position -b.position,
        },
        {
          title: '合同期(年)',
          align: 'center',
          width: 120,
          dataIndex: 'contractPeriod',
          sorter: (a,b) => a.contractPeriod -b.contractPeriod,
        },
        /* {
          title: '合同附件',
          align: 'center',
          dataIndex: 'uploadPath',
          scopedSlots: { customRender: 'fileSlot' },
        }, */
        {
          title: '试用期(月)',
          align: 'center',
          width: 120,
          dataIndex: 'probationPeriod',
          sorter: (a,b) => a.probationPeriod -b.probationPeriod,
        },
        {
          title: '员工类型',
          align: 'center',
          width: 120,
          dataIndex: 'workingHours_dictText',
          sorter: (a,b) => a.workingHours_dictText -b.workingHours_dictText,
        },
        {
          title: '年薪(元)',
          align: 'center',
          width: 120,
          dataIndex: 'annualSalary',
          sorter: (a,b) => a.annualSalary -b.annualSalary,
        },
        {
          title: '月薪(元)',
          align: 'center',
          width: 120,
          dataIndex: 'monthlySalary',
          sorter: (a,b) => a.monthlySalary -b.monthlySalary,
        },
        {
          title: '预期报到日期',
          align: 'center',
          dataIndex: 'arrivalTime',
          width: 120,
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
          sorter: (a,b) => a.arrivalTime -b.arrivalTime,
        },
        {
          title: '是否接受OFFER',
          align: 'center',
          width: 120,
          dataIndex: 'isConfirmOffer',
          customRender: function (text) {
            return !text ? '' : text =='N' ? '否' : '是'
          },
          sorter: (a,b) => a.isConfirmOffer -b.isConfirmOffer,
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'statusId_dictText',
          fixed: 'right',
          width: 100,
          sorter: (a,b) => a.statusId_dictText -b.statusId_dictText,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 120,
          scopedSlots: { customRender: 'action' },
          fixed: 'right'
        },
      ],
      url: {
        list: '/online/api/listHrOffer',
        updateHrOffer: '/online/api/updateHrOffer',
        delete: '/online/api/deleteHrOffer',
        deleteBatch: '/online/api/deleteHrOfferBatch',
        /* exportXlsUrl: '/online/api/exportHrOfferXls',
        importExcelUrl: '/online/api/importHrOfferExcel', */
        exportXlsUrl: '/online/api/export/exportXlsHrOffer',
        importExcelUrl: '/online/api/import/importXlsHrOffer',
        createClmPublicApproval: '/online/api/createClmPublicApproval',
        getClmApproval: '/online/api/getClmApproval',
        getOfferEmailTemplate: '/online/api/getOfferEmailTemplate',
        sendOfferEmail: '/online/api/sendOfferEmail',
      },
      dictOptions: {},
      offerId: '',
      name: '',
      record: {},
      isConfirmOffer: '',
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {},
    
    handleSubmit(id) {
      getAction(this.url.createClmPublicApproval, { id: id,configId:'9001' }).then((res) => {
        if (res.success) {
          this.$message.success('提交成功')
          this.loadData()
        }else{
            this.$message.error("提交失败!" + res.message)
          }
      })
    },
    handleSteps(id) {
      this.data2 = []
      this.apprVisible = true
      getAction(this.url.getClmApproval, { bizEntityName: 'HrOffer', bizEntityKey: id }).then((res) => {
        if (res.success) {
          let jsonStr = res.result.spRecord
          this.data2 = JSON.parse(jsonStr) //转换为json对象
          this.data2.forEach((record, index) => {
            record.id = index + 1
            if (record.sp_status == '1') {
              record.statusName = ['审批中']
            } else if (record.sp_status == '2') {
              record.statusName = ['已同意']
            } else if (record.sp_status == '3') {
              record.statusName = ['已驳回']
            } else {
              record.statusName = ['已转审']
            }
            if (record.approverattr == '1') {
              record.approverattrName = '或签'
            } else if (record.approverattr == '2') {
              record.approverattrName = '会签'
            }
            record.details.forEach((item, index) => {
              var ss = item.sptime * 1000
              item.dateTime = this.changeTimeStamp(ss)
              if (item.sp_status == '1') {
                item.statusName = ['审批中']
              } else if (item.sp_status == '2') {
                item.statusName = ['已同意']
              } else if (item.sp_status == '3') {
                item.statusName = ['已驳回']
              } else {
                item.statusName = ['已转审']
              }
              item.userName = item.approver.userid
	      if(item.approver.username)item.userName = item.approver.username
            })
          })
        }
      })
    },
    handleSend(record) {
      this.emailVisible = true
      this.offerId = record.id
      getAction(this.url.getOfferEmailTemplate, { receiveUserName: record.candidateId_dictText }).then((res) => {
        if (res.success) {
          let content = res.result.content
          content = content.replace('#姓名#',record.candidateId_dictText)
          content = content.replace('#基薪#',record.salaryBaseId)
          content = content.replace('#保障金#',record.salaryEnsureId)
          content = content.replace('#月薪#',record.monthlySalary)
          content = content.replace('#有效期#',record.expiryDate)
          this.content = content
        }
      })
    },
    sendEmail() {
      getAction(this.url.sendOfferEmail, { id: this.offerId }).then((res) => {
        if (res.success) {
          this.emailVisible = false
          this.$message.success('发送成功!')
          this.loadData()
        }
      })
    },
    handleConfirmOffer(record) {
      this.record = record
      this.name = record.candidateId_dictText
      this.confirmOfferVisible = true
    },
    confirmOffer() {
      this.record.isConfirmOffer = this.isConfirmOffer
      getAction(this.url.updateHrOffer, this.record).then((res) => {
        if (res.success) {
          this.confirmOfferVisible = false
          this.isConfirmOffer = 'N'
          this.$message.success('操作成功!')
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
</style>
